using LCL.Caching;
using LCL.Config;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LCL.LData
{
    /// <summary>
    /// 获取数据库信息类的接口定义。
    /// </summary>
    public interface IDbObject
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        DbConfigInfo DbConfig { get; set; }
        /// <summary>
        /// 获取数据库列表
        /// </summary>
        /// <returns></returns>
        List<DataBaseModel> GetDBList(string host);
        /// <summary>
        /// 获取表名列表
        /// </summary>
        /// <returns></returns>
        List<TableModel> GetTables(string dbName, bool iscol = false);
        /// <summary>
        /// 根据表名获取表详细包括字段信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        TableModel GetTableInfo(string dbName, string tableName);
        /// <summary>
        /// 获取表字段信息
        /// </summary>
        /// <returns></returns>
        List<ColumnInfo> GetColumnInfoList(string dbName, string tableName);
        /// <summary>
        /// 获取表关系信息
        /// </summary>
        /// <returns></returns>
        List<TableRelation> GetTableRelation(string tableName);
        /// <summary>
        /// 把数据库里所有的ID列添加主键和自增列
        /// </summary>
        void AddTableByKey(string dbName);
        /// <summary>
        /// 添加表或字段说明
        /// </summary>
        /// <param name="addbyupdate">存在就更新</param>
        /// <param name="describe">描述</param>
        /// <param name="tableName">表名</param>
        /// <param name="columnName">列名，为空修改表说明</param>
        void ExtendedProperty(bool addbyupdate, string describe, string tableName, string columnName);
    }

    public class DbModelBase : MarshalByRefObject
    {
        protected string CacheKey_DBLIST = "CACHEKEY_{0}_DBLIST";
        protected string CacheKey_TABLELIST = "CACHEKEY_{0}_TABLELIST";
        protected string CacheKey_TABLE_INFO = "CACHEKEY_{0}_{1}_INFO";
        protected string CacheKey_TABLE_COL = "CACHEKEY_{0}_{1}_COL";
        protected LCache cache = LCache.Instance;
        public ArrayList ArrNoDblist
        {
            get
            {
                ArrayList ali = new ArrayList();
                ali.Add("master");
                ali.Add("tempdb");
                ali.Add("model");
                ali.Add("msdb");
                ali.Add("test");
                ali.Add("mysql");
                return ali;
            }
        }
        public ArrayList ArrNoTableslist
        {
            get
            {
                ArrayList ali = new ArrayList();
                ali.Add("__MigrationHistory");
                ali.Add("sysdiagrams");
                ali.Add("model");
                ali.Add("abc");
                ali.Add("aa");
                return ali;
            }
        }
        public ArrayList ArrTree
        {
            get
            {
                ArrayList ali = new ArrayList();
                ali.Add("pid");
                ali.Add("upid");
                return ali;
            }
        }
        public void Run(Action action)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                try
                {
                    action();
                }
                catch { }
            });
        }
        #region DbHelper
        public DbConfigInfo DbConfig{ get; set; }
        public DbAccesser DbHelper
        {
            get
            {
                DbAccesser db = new DbAccesser(DbConfig.ConnectionString, DbConfig.DbType);
                return db;
            }
        }
        public IDbObject DbObject
        {
            get
            {
                return LCL.LData.DbObject.GetDbModel(DbConfig);
            }
        }
        #endregion

        //对类型名称 解析
        protected void TypeNameProcess(string strName, out string TypeName, out string Length, out string Preci, out string Scale)
        {
            TypeName = strName;
            int n = strName.IndexOf("(");
            Length = "";
            Preci = "";
            Scale = "";
            if (n > 0)
            {
                TypeName = strName.Substring(0, n);
                switch (TypeName.Trim().ToUpper())
                {
                    //只有大小(M)
                    case "TINYINT":
                    case "SMALLINT":
                    case "MEDIUMINT":
                    case "INT":
                    case "INTEGER":
                    case "BIGINT":
                    case "TIMESTAMP":
                    case "CHAR":
                    case "VARCHAR":
                        {
                            int m = strName.IndexOf(")");
                            Length = strName.Substring(n + 1, m - n - 1);
                        }
                        break;
                    case "FLOAT"://(M,D)
                    case "DOUBLE":
                    case "REAL":
                    case "DECIMAL":
                    case "DEC":
                    case "NUMERIC":
                        {
                            int m = strName.IndexOf(")");
                            string strlen = strName.Substring(n + 1, m - n - 1);
                            int i = strlen.IndexOf(",");
                            Length = strlen.Substring(0, i);
                            Scale = strlen.Substring(i + 1);
                        }
                        break;
                    case "ENUM"://(M1,M2,M3)
                    case "SET":
                        {
                        }
                        break;
                    default:
                        break;
                }
            }

        }
    }


}
